#!/bin/bash

# Configure Global`s Variables
VERSION="0.4"
NAME="Ports for Slackbuilds"
BUILDS="/var/cache/portsbuilds"
DIA="/bin/dialog"
SYNC="/usr/bin/rsync"
RAND=`date +%d%m%y%H%M%N`
TMP="/tmp/.$RAND"
SED="/bin/sed"

#Function check_depends
check_depends()
{
	#Check dialog program istalled
	if [ ! -f "$DIA" ]; then 
		echo 'Please install DIALOG'
		exit 0;
	fi

	#Check rsync program installed
	if [ ! -f "$SYNC" ]; then 
		echo 'Please install RSYNC'
		exit 0;
	fi

	#Check sed program istalled
	if [ ! -f "$SED" ]; then 
		echo 'Please install SED'
		exit 0;
	fi

	#Create temporary directory
	mkdir $TMP

	#Identify Slackware Version
	cat /etc/slackware-version | cut -f2 -d" " > $TMP/version
	cat $TMP/version | cut -f1,2 -d. > $TMP/version.ok
	S_VERSION=`cat $TMP/version.ok`

	#If first run, execute update_builds
	if [ ! -d $BUILDS ]; then 
		update_builds
		echo $BUILDS
		sleep 5
	fi

}

search_package()
{
	#search PACKAGE!!!
	$DIA	\
	--title "$NAME - $VERSION"	\
	--inputbox "Enter name package for search"	\
	0 0 2> $TMP/SEARCH.txt

	if [ $? = 1 ]; then
		main_menu
	fi
	
	SPACKAGE=`cat $TMP/SEARCH.txt`
	clear
	find $BUILDS/ -name "*$SPACKAGE*" -type d 1> $TMP/SEARCH_LIST.txt
	#find $BUILDS/ -name "*$SPACKAGE" 1> $TMP/SEARCH_LIST.txt
	cat $TMP/SEARCH_LIST.txt | cut -f5,6 -d"/" > $TMP/SEARCH_FILTER.txt

	COUNT_SEARCH=`wc -l $TMP/SEARCH_FILTER.txt | cut -f1 -d" "`
	echo "$COUNT_SEARCH"

	if [ $COUNT_SEARCH == "0" ]; then
		$DIA	\
		--title "$NAME - $VERSION"	\
		--msgbox "No package search"	\
		0 0
		search_package	
	fi

	cat $TMP/SEARCH_FILTER.txt

	paste $TMP/SEARCH_FILTER.txt $TMP/SEARCH_FILTER.txt -d" " > $TMP/SEARCH_FILTER_MENU.txt


	PACKAGE=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Select de package'	\
		0 0 0	\
		$(cat $TMP/SEARCH_FILTER_MENU.txt))
	
	if [ $? = 1 ]; then
		main_menu
	fi
	
	echo $PACKAGE > $TMP/PACKAGE_SEARCH.txt
	SERIE=`cat $TMP/PACKAGE_SEARCH.txt | cut -f1 -d"/"`
	PACKAGE=`cat $TMP/PACKAGE_SEARCH.txt | cut -f2 -d"/"`

	info_package
}


#Function update_slackbuilds
update_builds()
{
	#run rsync from slackbuilds.org website to local machine. After rsync back main_menu
	$DIA	\
	--title "$NAME - $VERSION"	\
	--infobox "Wait update local builds"	\
	0 0 
	$SYNC -r rsync://rsync.slackbuilds.org/slackbuilds/$S_VERSION/* $BUILDS && echo "1" > $TMP/update.txt
        STATUS_UPDATE=`cat $TMP/update.txt`
	if [ $STATUS_UPDATE = "1" ]; then
		$DIA	\
		--title "$NAME - $VERSION"	\
		--msgbox "Builds Updated"	\
		0 0
		
		# Create control file for update
		date +%m-%d-%Y-%H:%M > $BUILDS/last_update.txt
		
		# Call main_menu
		main_menu
	fi
	$DIA	\
	--title "$NAME - $VERSION"	\
	--msgbox "Update builds Again"	\
	0 0
	update_builds
}


#Function select_package
select_package()
{
	#Select the program to BUILD
	ls -la $BUILDS/$SERIE | grep -v ".tar*" | cut -f2 -d":" > $TMP/saida.txt
	cat $TMP/saida.txt | cut -f2 -d" " > $TMP/saida1.txt
	sed 1d $TMP/saida1.txt > $TMP/saida.txt
	sed 1d $TMP/saida.txt > $TMP/saida1.txt
	cp $TMP/saida1.txt $TMP/saida.txt
	paste $TMP/saida.txt $TMP/saida1.txt -d\ > $TMP/lista.txt

	PACKAGE=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Select de package'	\
		0 0 0	\
		$(cat $TMP/lista.txt))
	
	if [ $? = 1 ]; then
		select_serie
	fi
	info_package
}

#Function build_package
build_package()
{
	echo $PACKAGE
	#build PACKAGE, but don`t install!!!
	$DIA	\
	--title "$NAME - $VERSION"	\
	--infobox "Wait download source and building: $PACKAGE"	\
	0 0 

	cd $BUILDS/$SERIE/$PACKAGE
	pwd
	cat $PACKAGE.info | grep DOWNLOAD= | cut -f2 -d"\"" > $TMP/url.txt

	clear
	echo "DOWNLOADING......"
	sleep 2
	wget -i $TMP/url.txt 
	echo "DOWNLOADING FINISHED"
	sleep 2

	clear
	echo "BUILDING PACKAGE......"
	sleep 2

	chmod +x $PACKAGE.SlackBuild
	./$PACKAGE.SlackBuild 1> /dev/null
	
	clear
	echo "INSTALLING BUILDING"
	sleep 2

	cd $BUILDS/$SERIE/$PACKAGE

	ARCH_PACK=`cat $BUILDS/$SERIE/$PACKAGE/$PACKAGE.SlackBuild | grep ^ARCH | cut -f2 -d= | cut -f1 -d" "`
	VERSION_PACK=`cat $BUILDS/$SERIE/$PACKAGE/$PACKAGE.SlackBuild | grep ^VERSION | cut -f2 -d= | cut -f1 -d" "`

	installpkg /tmp/$PACKAGE-$VERSION_PACK-*.tg*

	echo "PACKAGE INSTALLED"
	sleep 2
	
	main_menu
}

#Function select_serie
select_serie()
{
	#Create menu serie
	SERIE=$($DIA --stdout				\
		--title "$NAME - $VERSION"	\
		--menu 'Select de series'	\
		0 0 0				\
		academic	'academic'	\
		accessibility	'accessibility'	\
		audio 		'audio'		\
		business	'business'	\
		desktop		'desktop'	\
		development	'development'	\
		games		'games'		\
		graphics	'graphics'	\
		libraries	'libraries'	\
		misc		'misc'		\
		multimedia	'multimedia'	\
		network		'network'	\
		office		'office'	\
		system		'system'	)

	if [ $? = 1 ]; then
		main_menu
	fi
	select_package
}

info_package()
{
	cat $BUILDS/$SERIE/$PACKAGE/slack-desc | grep $PACKAGE | cut -f2,3,4,5,6 -d: > $TMP/$PACKAGE_DESC.txt

	#View description about package
	DESC=$($DIA --stdout				\
		--title "$NAME - $VERSION"	\
		--textbox "$TMP/$PACKAGE_DESC.txt"	\
		0 0	\
		--yesno "Build $PACKAGE?" 0 0
		)
	if [ $? = 1 ]; then
		select_serie
	fi
		build_package

}

#Function main_menu
main_menu()
{
	#Create main menu
	OPTION=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Which option would you like?'	\
		0 0 0                                     \
		Update       'Update local builds from slackbuilds.org'	\
		Build        'Build a new package from slackbuilds.org'	\
		Search       'Search package from slackbuilds.org'	\
		About	     "About $NAME"	\
		Exit	     "Exit $NAME")		


	if [ $? = 1 ]; then
		exit_
	fi
	if [ $OPTION == 'Update' ]; then
		update_builds
	elif [ $OPTION == "Build" ]; then
		select_serie
	elif [ $OPTION == "About" ]; then
		about
	elif [ $OPTION == "Search" ]; then
		search_package
	elif [ $OPTION == "Exit" ]; then
		exit_ 
	fi
}

#Function about
about()
{
	$DIA	\
	--title "$NAME - $VERSION"	\
	--textbox /usr/share/doc/portsbuilds/README	\
	0 0 
	main_menu
}

#Function exit_
exit_()
{
	#Remove temporary variables and exit program
	rm -rf $TMP
	clear
	exit 0;
}

#Function main
main()
{
	#Initialize program
	check_depends
	main_menu
}

main
